package lib.quasar.widget.sign;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import lib.quasar.util.DimenUtil;
import lib.quasar.widget.R;
import lib.quasar.widget.paint.PaintManager;

/**
 * description: 角标
 * created by kalu on 2018/5/11 17:14
 */
public final class SignView extends View {

    private int bgColor = Color.RED;
    private float textSize = DimenUtil.sp2px(10f);

    private String mText = "重点";

    private boolean locationLeft;
    private boolean locationRight;

    /***********************************************************************/

    public SignView(Context context) {
        this(context, null, 0);
    }

    public SignView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SignView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        //获取所有的自定义属性
        TypedArray a = null;
        try {
            a = context.obtainStyledAttributes(attrs, R.styleable.SignView);
            bgColor = a.getColor(R.styleable.SignView_sv_bg_color, bgColor);
            textSize = a.getDimension(R.styleable.SignView_sv_text_size, textSize);

            locationLeft = a.getBoolean(R.styleable.SignView_sv_location_left, false);
            locationRight = a.getBoolean(R.styleable.SignView_sv_location_left, false);
        } catch (Exception e) {
        } finally {
            if (null == a) return;
            a.recycle();
        }
    }

    /***********************************************************************/

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

        if (TextUtils.isEmpty(mText)) return;

        if (locationLeft) {

            canvas.save();
            final Path path = new Path();
            path.moveTo(0f, 0f);
            path.lineTo(getWidth(), 0);
            path.lineTo(0, getHeight());
            path.lineTo(0f, 0f);
            path.close();
            canvas.drawPath(path, PaintManager.getTextPaint(bgColor));
            canvas.restore();

            canvas.save();
            canvas.rotate(-45, getWidth() / 2, getHeight() / 2);
            float x = getWidth() * 0.5f * 0.75f;
            float y = getHeight() * 0.5f * 0.75f;
            final Paint paint = PaintManager.getTextPaint(Color.WHITE, textSize, true);
            final float font1 = (paint.getFontMetrics().bottom - paint.getFontMetrics().top) * 0.3f;
            final float font2 = (paint.getFontMetrics().bottom - paint.getFontMetrics().top) * 0.1f;
            canvas.drawText(mText, x + font1, y + font2, paint);
            canvas.restore();
        } else if (locationRight) {

            canvas.save();
            final Path path = new Path();
            path.moveTo(0f, 0f);
            path.lineTo(getWidth() * 0.5f, 0);
            path.lineTo(getWidth(), getHeight() * 0.5f);
            path.lineTo(getWidth(), getHeight());
            canvas.drawPath(path, PaintManager.getTextPaint(bgColor));
            canvas.restore();

            canvas.save();
            canvas.rotate(45, getWidth() / 2, getHeight() / 2);
            float x = getWidth() * 0.5f * 0.75f;
            float y = getHeight() * 0.5f * 0.75f;
            final Paint paint = PaintManager.getTextPaint(Color.WHITE, textSize, true);
            final float font1 = (paint.getFontMetrics().bottom - paint.getFontMetrics().top) * 0.3f;
            final float font2 = (paint.getFontMetrics().bottom - paint.getFontMetrics().top) * 0.1f;
            canvas.drawText(mText, x + font1, y + font2, paint);
            canvas.restore();
        }
    }

    public void setSign(CharSequence string, int color) {
        mText = String.valueOf(string);
        bgColor = color;
        invalidate();
    }
}
